<?php
$GLOBALS['basePath'] = '../';
try {
	require_once '../class/NHANVIEN.php';
	require_once '../class/PHONGBAN.php';
	require_once '../class/common.php';

	if (!isset($_GET['Id'])) throw new Exception();
	
	$values = NHANVIEN::Read($_GET['Id']);
	$errors = array();
	
	// POST
	if (isset($_POST["Submit"])) {
		$values = $_POST;
		$values["MaNV"] = $_GET['Id'];
		$errors = NHANVIEN::Validate($values);
		
		if (empty($errors))	{
			NHANVIEN::Update($values);
			Redirect("index.php");
		}
	}
	$depts = PHONGBAN::Select();
}
 catch (Exception $e) {
	Redirect("/error.php");			
}
EchoHeader('Nhân viên', 'employee');
?>
<script type="text/javascript">
	$(function () {
		// init date-picker control
		$('#NgaySinh').datepicker({
			inline: true,
			dateFormat: "dd/mm/yy"
		});

		// highlight textbox
		$(':text').focus(function () {
			$(this).addClass('selected');
		});
		$(':text').blur(function () {
			$(this).removeClass('selected');
		});

		// form field hint
		$('.hint').append('<span class="pointer">&nbsp;</span>');
		$(':text, :checkbox, :password, select, :radio').focus(function () {
			$(this).parent().children('.hint').show();
		});
		$(':text, :checkbox, :password, select, :radio').blur(function () {
			$(this).parent().children('.hint').hide();
		});
	
	});
</script>	
<div class="primary box">
	<div class="box-inside">
		<div class="heading"><h2>Cập nhật thông tin nhân viên</h2></div>
		<div class="content">
		<form method="post" name="frmNhanVien" onSubmit="return onNhanVienSubmit()">
		<fieldset>
			<!-- HoTen -->
			<div class="field">
				<div class="label"><label for="HoTen">Họ tên</label></div>
				<div class="input">
					<input id="HoTen" name="HoTen" type="text" value="<?php echo GetValue($values, "HoTen"); ?>"  <?php echo GetErrorClass($errors, "HoTen"); ?>/>
					<?php echo GetError($errors, "HoTen"); ?><span class="hint">Họ tên đầy đủ của nhân viên</span>
				</div>
			</div>
		
			<!-- NgaySinh -->
			<div class="field">
				<div class="label"><label for="NgaySinh">Ngày sinh</label></div>
				<div class="input">
					<input type="text" name="NgaySinh" id="NgaySinh" value="<?php echo GetValue($values, "NgaySinh"); ?>"  <?php echo GetErrorClass($errors, "NgaySinh"); ?>/>
					<?php echo GetError($errors, "NgaySinh"); ?><span class="hint">Ngày sinh, định dạng dd/mm/yyyy</span>
				</div>
			</div>
		
			<!-- DiaChi -->
			<div class="field">
				<div class="label"><label for="DiaChi">Địa chỉ</label></div>
				<div class="input">
					<input type="text" name="DiaChi" id="DiaChi" value="<?php echo GetValue($values, "DiaChi"); ?>" <?php echo GetErrorClass($errors, "DiaChi"); ?>/>
					<?php echo GetError($errors, "DiaChi"); ?><span class="hint">Địa chỉ thường trú</span>
				</div>
			</div>
				
			<!-- Phai -->
			<div class="field">
				<div class="label">Phái</div>
				<div class="input">
					<input type="radio" name="Phai" id="Nam" value="0" <?php echo GetCheck($values, "Phai", 0); ?>/>
					<label for="Nam">Nam</label>
					<input type="radio" name="Phai" id="Nu" value="1" <?php echo GetCheck($values, "Phai", 1); ?>/>
					<label for="Nu">Nữ</label>
					<?php echo GetError($errors, "Phai"); ?><span class="hint">Giới tính</span>
				</div>
			</div>
			
			<!-- Luong -->
			<div class="field">
				<div class="label"><label for="Luong">Lương</label></div>
				<div class="input">
					<input type="text" name="Luong" id="Luong" value="<?php echo GetValue($values, "Luong"); ?>" 
					<?php echo GetErrorClass($errors, "Luong"); ?> onkeypress="return checkNumber(this,10,event);" />
					<?php echo GetError($errors, "Luong"); ?><span class="hint">Lương tháng, tính theo VNĐ</span>
				</div>
			</div>
			
			<!-- Phong -->
			<div class="field">
				<div class="label">Phòng</div>
				<div class="input">
					<select name="Phong">
						<?php
							foreach ($depts as $dept)
								echo sprintf('<option value="%d" %s>%s</option>', 
									$dept['MaPhong'], GetSelect($values, 'Phong', $dept['MaPhong']), $dept['TenPhong']);
						?>
					</select>
					<?php echo GetError($errors, "Phong"); ?><span class="hint">Phòng ban trực thuộc</span>
				</div>
			</div>
		</fieldset>

		<input type="submit" name="Submit" value="Lưu" class="button"/>
		<a href="index.php" class="button">Hủy</a>
		</form>
		</div>
	</div>
</div>
<?php EchoFooter(); ?>